/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import upc.Students;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;


/**
 *
 * @author Darwin
 */
@Stateless
@Path("upc.students")
public class StudentsFacadeREST extends AbstractFacade<Students> {
    @PersistenceContext(unitName = "PC2_RamirezDarwinPU")
    private EntityManager em;

    public StudentsFacadeREST() {
        super(Students.class);
    }

    @POST
    @Path("POST/Students")
    @Override
    @Consumes({"application/xml", "application/json"})
    public void create(Students entity) {
        super.create(entity);
    }

    @PUT
    @Path("PUT/Students/{id}")
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(Students entity) {
        super.edit(entity);
    }
/*
    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") String id) {
        super.remove(super.find(id));
    }
    
/*    @GET
    @Path("student")
    @Produces({"application/xml", "application/json"})
    public List<Students> findAllStudents() {
        return super.findAll();
    }*/
    
    @GET
    @Path("GET/Students/{id}")
    @Produces({"application/xml", "application/json"})
    public Students find(@PathParam("id") String id) {
        return super.find(id);
    }

    @GET
    @Path("GET/Students")
    @Produces({"application/xml", "application/json"})
    public List<Students> findAll() {
        return super.findAll();
    }

    @PUT
    @Consumes({"application/xml", "application/json"})
    public void editStudents(Students entity) {
        super.edit(entity);
    }
    
    @DELETE
    @Path("DELETE/Students/{id}")
    public void removeStudent(@PathParam("id") String id) {
        super.remove(super.find(id));
    }
/*    
    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Students> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }
*/
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
    @GET
    @Path("Query/{lastName}/{firstName}")
    @Produces({"application/xml", "application/json"})
    public List<Students> consulta1(@PathParam("lastName") String lastname, @PathParam("firstName") String firstname) {
        Query q = em.createQuery("SELECT s FROM Students s WHERE s.firstname = :firstName AND s.lastname = :lastName");
        q.setParameter("lastName", lastname);
        q.setParameter("firstName", firstname);
        return q.getResultList();
    }
    
    
}
